package com.cloudpivot.receivales.contract.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.contract.configuration.CrmConfig;
import com.cloudpivot.receivales.contract.entity.erpcontract.CrmContractPull;
import com.cloudpivot.receivales.contract.domain.crmcontract.CrmContract;
import com.cloudpivot.receivales.contract.domain.crmcontract.CrmContractItem;
import com.cloudpivot.receivales.contract.domain.oa.OAContract;
import com.cloudpivot.receivales.contract.service.OAEntityToCrmEntity;
import com.cloudpivot.receivales.contract.service.http.HttpAPIServiceForCrm;
import com.cloudpivot.receivales.contract.util.StrUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OAEntityToCrmEntityImpl implements OAEntityToCrmEntity {

    @Autowired
    private CrmConfig crmConfig;
    @Autowired
    private HttpAPIServiceForCrm httpApi;

    /**
     * 从u8到crm (oa--u8--crm)
     *
     * @param entitys
     * @return
     */
    @Override
    public CrmContract toConvertEntityByU8(List<CrmContractPull> entitys,String version) {
        CrmContract crmContract = new CrmContract();
        List<CrmContractItem> list = new ArrayList<>();
        //取表头
        CrmContractPull entity = entitys.get(0);
        //时间转换
        crmContract.setNew_dstartdate(StrUtil.convertGMTDateTime(entity.getStrContractStartDate()));
        crmContract.setNew_denddate(StrUtil.convertGMTDateTime(entity.getStrContractEndDate()));
        crmContract.setNew_dsigndate(StrUtil.convertGMTDateTime(entity.getStrContractOrderDate()));
        crmContract.setNew_total(entity.getDblTotalCurrency());
        crmContract.setNew_desc(entity.getStrContractDesc());
        crmContract.setNew_name(entity.getStrContractID());
        crmContract.setNew_contract_name(entity.getStrContractName());
        crmContract.setNew_cunit(entity.getCdefine11());
        crmContract.setNew_cdepname(entity.getCdepname());
        crmContract.setNew_dblexchange(entity.getDblExchange());
        crmContract.setNew_exchname(entity.getStrCurrency());
        crmContract.setNew_ccusname(entity.getCcusname());
        crmContract.setNew_cpersonname(entity.getCPersonName());
        crmContract.setNew_repair(entity.getStrRepair());
        //制单人
        crmContract.setNew_cmaker(entity.getStrSetupPerson());
        //生效人
        crmContract.setNew_csign(entity.getStrSetupPerson());
        crmContract.setNew_cdefine12(entity.getCdefine12());
        crmContract.setNew_chdefine2(entity.getChdefine2());
        crmContract.setNew_cdefine13(entity.getCdefine13());
        crmContract.setNew_chdefine1(entity.getChdefine1());
        crmContract.setNew_cendcustomer(entity.getCdefine10());
        crmContract.setNew_new_crmcustomeridodatabind(String.format("/accounts(%s)", entity.getCcusdefine7()));
        //公司要转换
        crmContract.setNew_company(companyNameToGuid(entity.getCdefine11()));
        //默认转换为博圣
        crmContract.setNew_ccid("111");
        for (int i = 0; i < entitys.size(); i++) {
            CrmContractPull cs = entitys.get(i);
            CrmContractItem crmContractItem = new CrmContractItem();
            crmContractItem.setNew_name(cs.getRowno());
            crmContractItem.setNew_cinvname(cs.getStrName());
            crmContractItem.setNew_cinvcode(cs.getStrInvoiceID());
            crmContractItem.setNew_iquantity(cs.getDblQuantity());
            crmContractItem.setNew_cunitname(cs.getStrMeasureUnit());
            crmContractItem.setNew_itaxrate(cs.getDblTaxRatio());
            crmContractItem.setNew_itaxprice(cs.getDblPrice());
            crmContractItem.setNew_isum(cs.getDblSum());
            crmContractItem.setNew_inatsum(cs.getDblSumRMB());
            crmContractItem.setNew_dstart(StrUtil.convertGMTDateTime(cs.getDtStartDate()));
            crmContractItem.setNew_dend(StrUtil.convertGMTDateTime(cs.getDtEndDate()));
            crmContractItem.setNew_dzbdate(StrUtil.convertGMTDateTime(cs.getCbdefine1()));
            crmContractItem.setNew_cmemos(cs.getStrMemo());
            if(version.equals("3")){
                crmContractItem.setNew_system_version("3");
                //V3版本U8Sql转录为CRM时的特殊字段
                crmContractItem.setNew_group_id(cs.getCbdefine14());
                crmContractItem.setNew_group_name(cs.getCbdefine15());
                crmContractItem.setNew_main_key_product_code(cs.getCbdefine16());
                crmContractItem.setNew_parent_group_id(cs.getCbdefine17());
                crmContractItem.setNew_propor(0.00);
            }
            //交付时间
            crmContractItem.setNew_dcheckdate(null);
            crmContractItem.setNew_kehuodatabind(String.format("/accounts(%s)", cs.getCcusdefine7()));
            list.add(crmContractItem);
        }
        crmContract.setContractItems(list);
        return crmContract;
    }

    /**
     * 从oa到crm oa--crm
     *
     * @param oaContract
     * @return
     */
    @Override
    public CrmContract toConvertEntityByOA(OAContract oaContract) {
        return null;
    }

    private String companyNameToGuid(String name) {
        String url = String.format("%s%s", this.crmConfig.getCrmurl(), "new_companies?$filter=new_name eq '"+name+"'");
        try {
            url = StrUtil.urlEcode(url);
            String ret=httpApi.doGetCrm(url);
            if (StringUtils.isBlank(ret)) {
                return null;
            }else{
                JSONObject jsonObject = JSONObject.parseObject(ret);
                JSONArray jsonArray = jsonObject.getJSONArray("value");
                JSONObject jsonObject1 =(JSONObject) jsonArray.get(0);
                String res=String.format("/new_companies(%s)",jsonObject1.getString("new_companyid"));
                return res;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
